import 'package:flame/collisions.dart';
import 'package:flame/components.dart';

class BulletComponent extends RectangleComponent with HasGameRef {
  Vector2? dir;

  BulletComponent({
    this.dir,
    super.position,
    super.angle,
    super.anchor = Anchor.center,
    super.children,
    super.priority,
    super.paint,
    super.paintLayers,
    super.size,
  }) {//初始化参数
    size = size.x > 0 ? size : Vector2(5, 5);
  }

  @override
  Future<void> onLoad() async {
    add(RectangleHitbox());
    return super.onLoad();
  }

  @override
  void onRemove() {
    print("移除位置:$position");
    super.onRemove();
  }

  @override
  void update(double dt) {
    // 更新子弹位置，根据射击方向设置子弹的速度和运动方向
    //print("子弹x=$x y=$y  ${game.camera.viewport.position}");
    // 判断子弹是否超出屏幕，如果是则销毁子弹
    var cameraPosition = game.camera.viewport.position;
    //print("cameraPosition=${cameraPosition} game.size=${game.size}");
    if (x < cameraPosition.x ||
        x > cameraPosition.x + game.size.x ||
        y < cameraPosition.y ||
        y > cameraPosition.y + game.size.y) {
      removeFromParent();
      return;
    }
    if (dir != null) {
      position.add(dir! * dt * 500);
    }
  }

  bool isOutOfWorldBounds(Vector2 worldPosition) {
    final visibleWorldRect  = gameRef.camera.viewfinder.visibleGameSize;

    if (visibleWorldRect != null) {
      //return !visibleWorldRect.c(position);
    }
    return false;
  }


}
